package br.com.moto.ejb;
 
import br.com.moto.entity.FotoHome;
import java.util.List;
 
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
@Stateless
@LocalBean
public class FotoHomeSessionBean {

    @PersistenceContext
    private EntityManager em;    

    public List<FotoHome> retrieve() {
        Query query = em.createNamedQuery("FotoHome.findAll");
        return query.getResultList();
    }
    
    public List<FotoHome> getFotoByAlbumId(Integer albumId) {
        Query query = em.createNamedQuery("FotoHome.findByAlbumId").setParameter("albumId", albumId);
        return query.getResultList();
    }    
    
    public List<FotoHome> getFotoToUser(String username) {
        Query query = em.createNamedQuery("FotoHome.findByUsername").setParameter("username", username);
        return query.getResultList();
    }    
    
    public void delete(FotoHome fotoHome) {
        FotoHome excluir = em.find(FotoHome.class, fotoHome.getId());
        em.remove(excluir);
    }    
        
     public FotoHome create(FotoHome foto)
     {
        em.merge(foto);          
        return foto;
    }    

    public FotoHome update(FotoHome foto)
    {
           
        FotoHome updated = em.merge(foto);        
        System.out.println("FotoHome atualizado no bean");
        return updated;
    }
}
